﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace SharpAnalyzer.Entities
{
    [XmlRoot(ElementName = "CodeMetricsReport", Namespace = "")]
    public class CodeMetricsReport
    {
        public static readonly CodeMetricsReport Empty = new CodeMetricsReport();

        [XmlAttribute]
        public string Version
        {
            get;
            set;
        }

        [XmlAttribute]
        public string InputFile
        {
            get;
            set;
        }

        public List<Metric> Metrics { get; set; }

        public List<Project> Projects
        {
            get;
            set;
        }

        public void WriteTo(string reportFile)
        {
            using (Stream output = File.Open(reportFile, FileMode.OpenOrCreate, FileAccess.Write))
            {
                output.SetLength(0L);
                this.WriteTo(output);
                output.Flush();
            }
        }

        public void WriteTo(Stream output)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CodeMetricsReport));

            XmlWriterSettings settings = new XmlWriterSettings()
            {
                IndentChars = "  ",
                Indent = true,
                CloseOutput = false,
            };

            using (XmlWriter writer = XmlWriter.Create(output, settings))
                serializer.Serialize(writer, this);
        }
    }
}
